

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.io.UnsupportedEncodingException;

/**
 * jwt使用rsa非对称加解密工具类
 */
public class JwtUtil {
    private static final String SECRET="sdfdsfs"; // 密匙
    public static String generateJwtTokenHMAC(String payLoad){
        try {
            return  JWT.create()
                        .withIssuer("LHF")
                        .withClaim("userInfo", payLoad)
                        .sign(Algorithm.HMAC256(SECRET));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 校验token是否合法，合法返回负载信息，否则抛异常
     * @param token
     * @return
     */
    public static String checkToken(String token) {
        JWTVerifier verifier = null;
        try {
            verifier = JWT.require(Algorithm.HMAC256(SECRET))
                    .withIssuer("LHF")
                    .build();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        DecodedJWT jwt = verifier.verify(token);
        Claim claim = jwt.getClaim("userInfo");
        String payload = claim.asString();
        return payload;
    }
}
